﻿using SalesSys.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace SalesSys.BLL.Depot
{
    /// <summary>
    /// 锁库单
    /// </summary>
    public interface IStockLockBLL
    {
        /// <summary>
        /// 创建锁库单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        bool CreateByOrder(Order order);

        /// <summary>
        /// 核销锁库单
        /// </summary>
        /// <param name="deliverySheet">发货单</param>
        /// <returns></returns>
        bool WriteOffSheet(Order order);
    }

    public class StockLockBLL : BaseBLL<StockLock, Guid>, IStockLockBLL
    {

        /// <summary>
        /// 创建锁库单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public bool CreateByOrder(Order order)
        {
            var stock = Rep.Entities.Where(p => p.Id == order.Id && p.IsRelease == false).FirstOrDefault();
            if (stock != null)
                throw new NotImplementedException("该计划单锁库单已存在");
            var dcBll = new DepotCommodityBLL();
            using (var tran = new TransactionScope())
            {
                var providerBll = new ProviderBLL();
                foreach (var item in order.CommodityInfos)
                {
                    var stockLock = new StockLock();
                    var dComm = dcBll.GetDepotCommodity(order.Provider.Id, item.Commodity.Id);
                    stockLock.DepotCommodity = dComm;
                    stockLock.Count = item.Count;
                    stockLock.Weight = item.Weight;
                    stockLock.OrderId = order.Id;
                    stockLock.IsRelease = false;
                    Rep.Insert(stockLock);
                    //更新仓库商品库存
                    dComm.LockAmount += stockLock.Weight;
                    dComm.Amount -= stockLock.Weight;
                    dComm.LockCount += stockLock.Count;
                    dComm.Count -= stockLock.Count;
                    dComm.UpdateTime = DateTime.Now;
                    dcBll.Edit(dComm);
                    //var provider = order.Provider;
                    //provider.DepotRecords.Add(new DepotRecord
                    //{
                    //    Type = SalesSys.DAL.Models.DepotRecord.RecordType.Lock,
                    //    Commodity = item.Commodity,
                    //    Count = item.Count,
                    //    Price = item.Price,
                    //    Weight = (float)(item.Weight / item.Count),
                    //    BeforeTotalCount = dComm.Count,
                    //    BeforeTotalWeight = dComm.Amount,
                    //    AfterTotalCount = dComm.Count - item.Count,
                    //    AfterTotalWeight = dComm.Amount - item.Weight,
                    //    FromId = stockLock.Id,
                    //    TotalWeight = item.Weight
                    //});
                    //providerBll.Edit(provider);
                }
                tran.Complete();
            }
            return true;
        }

        /// <summary>
        /// 核销锁库单
        /// 查询该出货单所关联的锁库信息
        /// 根据出货单实际重量进行赋值
        /// </summary>
        /// <param name="deliverySheet">发货单</param>
        /// <returns></returns>
        public bool WriteOffSheet(Order order)
        {
            //查询所有
            var stockList = Rep.Entities.Where(p => p.OrderId == order.Id).ToArray();
            if (!stockList.Any())
            {
                return false;
            }
            using (var tran = new TransactionScope())
            {
                var providerBll = new ProviderBLL();
                var dcBll = new DepotCommodityBLL();
                foreach (var com in order.CommodityInfos)
                {
                    var dComm = dcBll.GetDepotCommodity(order.Provider.Id, com.Commodity.Id);
                    var stockLock = stockList.FirstOrDefault(p => p.DepotCommodity == dComm);
                    if (stockLock == null)
                        throw new NotImplementedException("锁库商品不存在");
                    stockLock.IsRelease = true;
                    //核销仓库商品库存
                    dComm.LockAmount -= stockLock.Weight;
                    dComm.Amount += stockLock.Weight;
                    dComm.LockCount -= stockLock.Count;
                    dComm.Count += stockLock.Count;
                    dComm.UpdateTime = DateTime.Now;
                    dcBll.Edit(dComm);
                    Rep.Update(stockLock);
                    //order.Provider.DepotRecords.Add(new DepotRecord
                    //{
                    //    Type = SalesSys.DAL.Models.DepotRecord.RecordType.Unlock,
                    //    Commodity = com.Commodity,
                    //    Count = com.Count,
                    //    Price = com.Price,
                    //    Weight = (float)(com.Weight / com.Count),
                    //    BeforeTotalCount = dComm.Count,
                    //    BeforeTotalWeight = dComm.Amount,
                    //    AfterTotalCount = dComm.Count + com.Count,
                    //    AfterTotalWeight = dComm.Amount + com.Weight,
                    //    FromId = stockLock.Id,
                    //    TotalWeight = com.Weight
                    //});
                    //providerBll.Edit(order.Provider);
                }
                tran.Complete();
            }
            return true;
        }
    }
}
